package cn.king.basic.controller;

import cn.king.basic.dto.PhoneCodeDto;
import cn.king.basic.exception.BusinessException;
import cn.king.basic.service.IVerifyCodeService;
import cn.king.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService iverifyCodeService;

    /**
     * 图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key")String key){
        try {
            String base64Str = iverifyCodeService.getImageCode(key);
            return AjaxResult.me().setObj(base64Str);
        }catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试！");
        }
    }

    /**
     * 手机验证码
     * @return
     */
    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody PhoneCodeDto phoneCodeDto){
        try {
            iverifyCodeService.PhoneCodeDto(phoneCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试！");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult getBinderSmsCode(@RequestBody PhoneCodeDto phoneCodeDto){
        try {
            iverifyCodeService.getBinderSmsCode(phoneCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后重试！");
        }
    }
}
